package com.android.djlmapp.adapter.kaika;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;

import com.android.djlmapp.R;

import java.util.ArrayList;
import java.util.HashMap;

public class SelectNumPopAdapter extends BaseAdapter {
    private Context context;
    private int type = 0; // 点击的类型
    private int selectPosition1 = 0; // 选中的条目
    private int selectPosition2 = 0; // 选中的条目
    private int selectPosition3 = 0; // 选中的条目
    private int selectPosition4 = 0; // 选中的条目
    private ArrayList<String> list = new ArrayList<>();

    HashMap<String, Boolean> selectStatus = new HashMap<String, Boolean>();//用于记录每个RadioButton的状态，并保证只可选一个

    public SelectNumPopAdapter(Context context) {
        this.context = context;
    }

    /**
     * 设置数据
     */
    public void addAll(int type, ArrayList<String> list) {
        this.list = list;
        this.type = type;
        notifyDataSetChanged();
    }

    /**
     * 清空数据
     */
    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    public void setSelectPosition(int position1, int position2, int position3, int position4) {
        this.selectPosition1 = position1;
        this.selectPosition2 = position2;
        this.selectPosition3 = position3;
        this.selectPosition4 = position4;

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.pop_select_item_list, null);
            viewHolder.rb_status = convertView
                    .findViewById(R.id.item_text);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //禁用radiobutton的点击事件，只通过listview的onItemClick事件设置选中位置变量，
        // 并且通过notifyDataSetChanged刷新列表。getView方法里当前项位置为选中项时设置radiobutton为选中状态。否则为未选中状态
        viewHolder.rb_status.setClickable(false);
        viewHolder.rb_status.setText(list.get(position));
        int selectPosition = 0;
        switch (type) {
            case 1:
                selectPosition = selectPosition1;
                break;
            case 2:
                selectPosition = selectPosition2;
                break;
            case 3:
                selectPosition = selectPosition3;
                break;
            case 4:
                selectPosition = selectPosition4;
                break;
        }
        setCheck(selectPosition, position, viewHolder.rb_status);
        return convertView;
    }

    class ViewHolder {
        RadioButton rb_status;
    }


    private void setCheck(int selectPosition, int position, RadioButton name) {
        if (selectPosition == position) {
            name.setChecked(true);
        } else {
            name.setChecked(false);
        }
    }
}
